//
//  BHCompetionDetailBottomView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/15.
//

import UIKit

@objc protocol BHCompetionDetailBottomViewDelegate: NSObjectProtocol {
    /**0报名 1签到  2群聊  3咨询*/
    func bottomDidSelectTypeAction(type: Int)
}

class BHCompetionDetailBottomView: UIView {
    
    @IBOutlet weak var signBtn: UIButton!
    @IBOutlet weak var applyView: UIView!
    @IBOutlet weak var applyEndTime_label: UILabel!
    
    @IBOutlet weak var state_top: NSLayoutConstraint!
    @IBOutlet weak var state_label: UILabel!
    @IBOutlet weak var askView: UIView!
    @IBOutlet weak var groupChatView: UIView!
    
    var delegate: BHCompetionDetailBottomViewDelegate?
    
    
    static func loadFromXib() -> BHCompetionDetailBottomView {
        let viewArray = Bundle.main.loadNibNamed("BHCompetionDetailBottomView", owner: nil, options: nil)
        let view: BHCompetionDetailBottomView = viewArray![0] as! BHCompetionDetailBottomView
        return view
    }
    
    var model: BHCompetitionDetailModel! {
        didSet {
            self.state_top.constant = model.status == 1 ? 8:15
            self.applyEndTime_label.isHidden = model.status != 1
            self.applyEndTime_label.text = "\(model.signupEnd)截止";
            self.applyView.backgroundColor = model.status == 1 ? .color_34CE98:.color_E2F8F0
            self.state_label.textColor = model.status == 1 ? .white:.color_34CE98

            if model.status == 0 {
                self.state_label.text = "报名已截止"
            }else if model.status == 2 {
                self.state_label.text = "已报名"
            }else if model.status == 3 {
                self.state_label.text = "赛事已结束"
            }else if model.status == 4 {
                self.state_label.text = "报名已满"
            }else {
                self.state_label.text = "立即报名"
            }
        }
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.signBtn.addLayerCorner(25, 0, .white)
        self.applyView.addLayerCorner(25, 0, .white)
        
        self.applyView.addViewAction {[weak self] (_) in
            self?.delegate?.bottomDidSelectTypeAction(type: 0)
        }
        
        self.groupChatView.addViewAction {[weak self] (_) in
            self?.delegate?.bottomDidSelectTypeAction(type: 2)
        }
        
        self.askView.addViewAction {[weak self] (_) in
            self?.delegate?.bottomDidSelectTypeAction(type: 3)
        }
    }
    
    static var bottomHeight: CGFloat {
        get {
            return CGFloat(81)+UIScreen.safeBottomHeight
        }
    }
    
    // 签到
    @IBAction func signInAct(_ sender: UIButton) {
        if sender.isSelected == true {
            return
        }
        self.delegate?.bottomDidSelectTypeAction(type: 1)

    }
    
}
